package com.baizhi.appcontroller;

import com.baizhi.entity.CmfzUser;
import com.baizhi.service.CmfzUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @Author: renpu
 * @Date: 2019/3/7 20:55
 * @Description:
 */

@RestController
@RequestMapping("AppUser")
public class AppUserController {

    @Autowired
    private CmfzUserService userService;

    @GetMapping("selectIdUser/{userId}")
    public CmfzUser selectIdUser(@PathVariable("userId") int userId){
        return userService.selectId(userId);
    }

    @GetMapping("selectIdUserT/{userId}")
    public CmfzUser selectIdUserT(@PathVariable("userId") int userId){
        return userService.selectIdT(userId);
    }

    @PostMapping("updateBaseMessage")
    public CmfzUser updateBaseMessage(CmfzUser user,MultipartFile myPicture, HttpSession session) throws IOException {
        String p=session.getServletContext().getRealPath("/img");
        File file=new File(p);
        if(!file.exists()){
            file.mkdir();
        }
        String oldName=myPicture.getOriginalFilename();
        String newName= UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
        //获取相对路径；
        String oop="/img/"+newName;

        //获取结对路径；
        String realPath=session.getServletContext().getRealPath(oop);
        System.out.println(realPath);
        myPicture.transferTo(new File(realPath));
       /* user.setUserId();*/
        user.setUserImage(realPath);
      /*  user.setUserNickName(userNickName);
        user.setUserName(userName);
        user.setUserAutoGraph(userAutoGraph);
        user.setUserProvince(userProvince);
        user.setUserCity(userCity);
        user.setUserSex(userSex);*/
        userService.updateBaseUserMessage(user);
        return user;
    }
}
